package com.jnu.bookkeep.ui.gallery;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.jnu.bookkeep.Data.Bill;
import com.jnu.bookkeep.Data.DataStorage;
import com.jnu.bookkeep.R;

import java.util.List;

public class GalleryFragment extends Fragment {

    public GalleryFragment() {
        // Required empty public constructor
    }

    private DataStorage dataStorage;
    private List<Bill> billList;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);

        TextView income_textView = rootView.findViewById(R.id.income);
        TextView expenditure_textView  = rootView.findViewById(R.id.expenditure);
        TextView sum_textView = rootView.findViewById(R.id.sum);

        dataStorage = new DataStorage(this.getContext());
        billList = dataStorage.loadData();

        float income = 0,expenditure = 0,sum;

        for(int i = 0;i < billList.size();i++){
            float tmp = billList.get(i).getAmount();
            if(tmp < 0){
                expenditure = expenditure + tmp;
            }
            else if(tmp >= 0){
                income = income + tmp;
            }
        }

        sum = income + expenditure;

        income_textView.setText("+"+String.valueOf(income));
        expenditure_textView.setText(String.valueOf(expenditure));
        if(sum >= 0){
            sum_textView.setText("+"+String.valueOf(sum));
        }
        else if(sum < 0){
            sum_textView.setText(String.valueOf(sum));
        }

        return rootView;
    }


}